package cn.lixu.system.controller;

import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.PageList;
import cn.lixu.system.domain.Permission;
import cn.lixu.system.query.PermissionQuery;
import cn.lixu.system.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限接口
 */
@RestController //controller层  返回的是json格式数据
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService ;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Permission> loadAll() {
        return permissionService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Permission loadById(@PathVariable("id") Long id) {
        return permissionService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            permissionService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param premission
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Permission premission) {
        try {
            if (premission.getId() == null) {
                permissionService.save(premission);
            } else {
                permissionService.update(premission);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param premissionQuery
     * @return
     */
    @PostMapping
    public PageList<Permission> queryPage(@RequestBody PermissionQuery premissionQuery) {
        return permissionService.queryPage(premissionQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            permissionService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }


}

